home *** CD-ROM | disk | FTP | other *** search
/ Network PC / Network PC.iso / amiga utilities / communication / internet / amitcp3.0b / netinclude / rpc / xdr.h < prev   
Encoding:
C/C++ Source or Header  |  1997-11-21  |  11.5 KB  |  308 lines

  1. /*
  2.  * $Id: xdr.h,v 1.2 1993/11/10 01:07:26 jraja Exp $
  3.  *
  4.  * $Log: xdr.h,v $
  5.  * Revision 1.2  1993/11/10  01:07:26  jraja
  6.  * ANSI prototypes.
  7.  * Added prototype for xdr_free().
  8.  *
  9.  */
  10. /* @(#)xdr.h    2.2 88/07/29 4.0 RPCSRC */
  11. /*
  12.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  13.  * unrestricted use provided that this legend is included on all tape
  14.  * media and as a part of the software program in whole or part.  Users
  15.  * may copy or modify Sun RPC without charge, but are not authorized
  16.  * to license or distribute it to anyone else except as part of a product or
  17.  * program developed by the user.
  18.  * 
  19.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  20.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  21.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  22.  * 
  23.  * Sun RPC is provided with no support and without any obligation on the
  24.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  25.  * modification or enhancement.
  26.  * 
  27.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  28.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  29.  * OR ANY PART THEREOF.
  30.  * 
  31.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  32.  * or profits or other special, indirect and consequential damages, even if
  33.  * Sun has been advised of the possibility of such damages.
  34.  * 
  35.  * Sun Microsystems, Inc.
  36.  * 2550 Garcia Avenue
  37.  * Mountain View, California  94043
  38.  */
  39. /*      @(#)xdr.h 1.19 87/04/22 SMI      */
  40.  
  41. /*
  42.  * xdr.h, External Data Representation Serialization Routines.
  43.  *
  44.  * Copyright (C) 1984, Sun Microsystems, Inc.
  45.  */
  46.  
  47. #ifndef __XDR_HEADER__
  48. #define __XDR_HEADER__
  49. #include <stdio.h>        /* for the FILE* */
  50. /*
  51.  * XDR provides a conventional way for converting between C data
  52.  * types and an external bit-string representation.  Library supplied
  53.  * routines provide for the conversion on built-in C data types.  These
  54.  * routines and utility routines defined here are used to help implement
  55.  * a type encode/decode routine for each user-defined type.
  56.  *
  57.  * Each data type provides a single procedure which takes two arguments:
  58.  *
  59.  *    bool_t
  60.  *    xdrproc(xdrs, argresp)
  61.  *        XDR *xdrs;
  62.  *        <type> *argresp;
  63.  *
  64.  * xdrs is an instance of a XDR handle, to which or from which the data
  65.  * type is to be converted.  argresp is a pointer to the structure to be
  66.  * converted.  The XDR handle contains an operation field which indicates
  67.  * which of the operations (ENCODE, DECODE * or FREE) is to be performed.
  68.  *
  69.  * XDR_DECODE may allocate space if the pointer argresp is null.  This
  70.  * data can be freed with the XDR_FREE operation.
  71.  *
  72.  * We write only one procedure per data type to make it easy
  73.  * to keep the encode and decode procedures for a data type consistent.
  74.  * In many cases the same code performs all operations on a user defined type,
  75.  * because all the hard work is done in the component type routines.
  76.  * decode as a series of calls on the nested data types.
  77.  */
  78.  
  79. /*
  80.  * Xdr operations.  XDR_ENCODE causes the type to be encoded into the
  81.  * stream.  XDR_DECODE causes the type to be extracted from the stream.
  82.  * XDR_FREE can be used to release the space allocated by an XDR_DECODE
  83.  * request.
  84.  */
  85. enum xdr_op {
  86.     XDR_ENCODE=0,
  87.     XDR_DECODE=1,
  88.     XDR_FREE=2
  89. };
  90.  
  91. /*
  92.  * This is the number of bytes per unit of external data.
  93.  */
  94. #define BYTES_PER_XDR_UNIT    (4)
  95. #define RNDUP(x)  ((((x) + BYTES_PER_XDR_UNIT - 1) / BYTES_PER_XDR_UNIT) \
  96.             * BYTES_PER_XDR_UNIT)
  97.  
  98. /*
  99.  * The XDR handle.
  100.  * Contains operation which is being applied to the stream,
  101.  * an operations vector for the paticular implementation (e.g. see xdr_mem.c),
  102.  * and two private fields for the use of the particular impelementation.
  103.  */
  104. typedef struct XDR {
  105.     enum xdr_op    x_op;        /* operation; fast additional param */
  106.     struct xdr_ops {
  107.         bool_t    (*x_getlong)(struct XDR *xdrs, long *longp);    /* get a long from underlying stream */
  108.         bool_t    (*x_putlong)(struct XDR *xdrs, long *longp);    /* put a long to " */
  109.         bool_t    (*x_getbytes)(struct XDR *xdrs, caddr_t addr, 
  110.                       u_int len);/* get some bytes from " */
  111.         bool_t    (*x_putbytes)(struct XDR *xdrs, caddr_t addr, 
  112.                       u_int len);/* put some bytes to " */
  113.         u_int    (*x_getpostn)(struct XDR *xdrs);/* returns bytes off from beginning */
  114.         bool_t  (*x_setpostn)(struct XDR *xdrs, u_int pos);/* lets you reposition the stream */
  115.         long *    (*x_inline)(struct XDR *xdrs, u_int len);/* buf quick ptr to buffered data */
  116.         void    (*x_destroy)(struct XDR *xdrs);    /* free privates of this xdr_stream */
  117.     } *x_ops;
  118.     caddr_t     x_public;    /* users' data */
  119.     caddr_t        x_private;    /* pointer to private data */
  120.     caddr_t     x_base;        /* private used for position info */
  121.     int        x_handy;    /* extra private word */
  122. } XDR;
  123.  
  124. /*
  125.  * A xdrproc_t exists for each data type which is to be encoded or decoded.
  126.  *
  127.  * The second argument to the xdrproc_t is a pointer to an opaque pointer.
  128.  * The opaque pointer generally points to a structure of the data type
  129.  * to be decoded.  If this pointer is 0, then the type routines should
  130.  * allocate dynamic storage of the appropriate size and return it.
  131.  * bool_t    (*xdrproc_t)(XDR *, caddr_t *);
  132.  */
  133. typedef    bool_t (* XDRFUN xdrproc_t)(XDR *, void *);
  134. /*
  135.  * xdr_string_t is for type casts in cases where a xdr function is called
  136.  * via a pointer. If the function happens to be xdr_string(), then it uses
  137.  * the last argument, which is passed as UINT_MAX, othervice the u_int is
  138.  * just ignored.
  139.  */
  140. typedef    bool_t (* XDRFUN xdr_string_t)(XDR *, void *, u_int);
  141.  
  142. /*
  143.  * Operations defined on a XDR handle
  144.  *
  145.  * XDR        *xdrs;
  146.  * long        *longp;
  147.  * caddr_t     addr;
  148.  * u_int     len;
  149.  * u_int     pos;
  150.  */
  151. #define XDR_GETLONG(xdrs, longp)            \
  152.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  153. #define xdr_getlong(xdrs, longp)            \
  154.     (*(xdrs)->x_ops->x_getlong)(xdrs, longp)
  155.  
  156. #define XDR_PUTLONG(xdrs, longp)            \
  157.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  158. #define xdr_putlong(xdrs, longp)            \
  159.     (*(xdrs)->x_ops->x_putlong)(xdrs, longp)
  160.  
  161. #define XDR_GETBYTES(xdrs, addr, len)            \
  162.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  163. #define xdr_getbytes(xdrs, addr, len)            \
  164.     (*(xdrs)->x_ops->x_getbytes)(xdrs, addr, len)
  165.  
  166. #define XDR_PUTBYTES(xdrs, addr, len)            \
  167.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  168. #define xdr_putbytes(xdrs, addr, len)            \
  169.     (*(xdrs)->x_ops->x_putbytes)(xdrs, addr, len)
  170.  
  171. #define XDR_GETPOS(xdrs)                \
  172.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  173. #define xdr_getpos(xdrs)                \
  174.     (*(xdrs)->x_ops->x_getpostn)(xdrs)
  175.  
  176. #define XDR_SETPOS(xdrs, pos)                \
  177.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  178. #define xdr_setpos(xdrs, pos)                \
  179.     (*(xdrs)->x_ops->x_setpostn)(xdrs, pos)
  180.  
  181. #define    XDR_INLINE(xdrs, len)                \
  182.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  183. #define    xdr_inline(xdrs, len)                \
  184.     (*(xdrs)->x_ops->x_inline)(xdrs, len)
  185.  
  186. #define    XDR_DESTROY(xdrs)                \
  187.     if ((xdrs)->x_ops->x_destroy)             \
  188.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  189. #define    xdr_destroy(xdrs)                \
  190.     if ((xdrs)->x_ops->x_destroy)             \
  191.         (*(xdrs)->x_ops->x_destroy)(xdrs)
  192.  
  193. /*
  194.  * Support struct for discriminated unions.
  195.  * You create an array of xdrdiscrim structures, terminated with
  196.  * a entry with a null procedure pointer.  The xdr_union routine gets
  197.  * the discriminant value and then searches the array of structures
  198.  * for a matching value.  If a match is found the associated xdr routine
  199.  * is called to handle that part of the union.  If there is
  200.  * no match, then a default routine may be called.
  201.  * If there is no match and no default routine it is an error.
  202.  */
  203. #define NULL_xdrproc_t ((xdrproc_t)0)
  204. struct xdr_discrim {
  205.     int    value;
  206.     xdrproc_t proc;
  207. };
  208.  
  209. /*
  210.  * In-line routines for fast encode/decode of primitve data types.
  211.  * Caveat emptor: these use single memory cycles to get the
  212.  * data from the underlying buffer, and will fail to operate
  213.  * properly if the data is not aligned.  The standard way to use these
  214.  * is to say:
  215.  *    if ((buf = XDR_INLINE(xdrs, count)) == NULL)
  216.  *        return (FALSE);
  217.  *    <<< macro calls >>>
  218.  * where ``count'' is the number of bytes of data occupied
  219.  * by the primitive data types.
  220.  *
  221.  * N.B. and frozen for all time: each data type here uses 4 bytes
  222.  * of external representation.
  223.  */
  224. #define IXDR_GET_LONG(buf)        ((long)ntohl((u_long)*(buf)++))
  225. #define IXDR_PUT_LONG(buf, v)        (*(buf)++ = (long)htonl((u_long)v))
  226.  
  227. #define IXDR_GET_BOOL(buf)        ((bool_t)IXDR_GET_LONG(buf))
  228. #define IXDR_GET_ENUM(buf, t)        ((t)IXDR_GET_LONG(buf))
  229. #define IXDR_GET_U_LONG(buf)        ((u_long)IXDR_GET_LONG(buf))
  230. #define IXDR_GET_SHORT(buf)        ((short)IXDR_GET_LONG(buf))
  231. #define IXDR_GET_U_SHORT(buf)        ((u_short)IXDR_GET_LONG(buf))
  232.  
  233. #define IXDR_PUT_BOOL(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  234. #define IXDR_PUT_ENUM(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  235. #define IXDR_PUT_U_LONG(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  236. #define IXDR_PUT_SHORT(buf, v)        IXDR_PUT_LONG((buf), ((long)(v)))
  237. #define IXDR_PUT_U_SHORT(buf, v)    IXDR_PUT_LONG((buf), ((long)(v)))
  238.  
  239. /*
  240.  * Function to free memory allocated by an xdr object
  241.  */
  242. void xdr_free(xdrproc_t proc, void * objp);
  243.  
  244. /*
  245.  * These are the "generic" xdr routines.
  246.  */
  247. extern bool_t XDRFUN    xdr_void(XDR * xdrs, void * dummy);
  248. extern bool_t XDRFUN    xdr_int(XDR * xdrs, int * ip);
  249. extern bool_t XDRFUN    xdr_u_int(XDR * xdrs, u_int * uip);
  250. extern bool_t XDRFUN    xdr_long(XDR * xdrs, long * lp);
  251. extern bool_t XDRFUN    xdr_u_long(XDR * xdrs, u_long * ulp);
  252. extern bool_t XDRFUN    xdr_short(XDR * xdrs, short * sp);
  253. extern bool_t XDRFUN    xdr_u_short(XDR * xdrs, u_short * usp);
  254. extern bool_t XDRFUN    xdr_bool(XDR * xdrs, bool_t * bp);
  255. extern bool_t XDRFUN    xdr_enum(XDR * xdrs, enum_t * ep);
  256. extern bool_t XDRFUN    xdr_array(XDR * xdrs, caddr_t * addrp, 
  257.                   u_int * sizep, u_int maxsize, 
  258.                   u_int elsize, xdrproc_t elproc);
  259. extern bool_t XDRFUN    xdr_bytes(XDR * xdrs, char ** cpp, u_int * sizep, 
  260.                   u_int maxsize);
  261. extern bool_t XDRFUN    xdr_opaque(XDR * xdrs, caddr_t cp, u_int cnt);
  262. extern bool_t XDRFUN    xdr_string(XDR * xdrs, char ** cpp, u_int maxsize);
  263. extern bool_t XDRFUN    xdr_union(XDR * xdrs, int * dscmp, char * unp,
  264.                   struct xdr_discrim * choices,
  265.                   xdrproc_t dfault);
  266. extern bool_t XDRFUN    xdr_char(XDR * xdrs, char * cp);
  267. extern bool_t XDRFUN    xdr_u_char(XDR * xdrs, u_char * ucp);
  268. extern bool_t XDRFUN    xdr_vector(XDR * xdrs, char * basep, u_int nelem, 
  269.                    u_int elemsize, xdrproc_t xdr_elem);
  270. extern bool_t XDRFUN    xdr_float(XDR *xdrs, float *fp);
  271. extern bool_t XDRFUN    xdr_double(XDR *xdrs, double *dp);
  272. extern bool_t XDRFUN    xdr_reference(XDR * xdrs, caddr_t * pp, u_int size,
  273.                       xdrproc_t proc);
  274. extern bool_t XDRFUN    xdr_pointer(XDR * xdrs, char ** objpp,
  275.                     u_int obj_size, xdrproc_t xdr_obj);
  276. extern bool_t XDRFUN    xdr_wrapstring(XDR * xdrs, char ** cpp);
  277.  
  278. /*
  279.  * Common opaque bytes objects used by many rpc protocols;
  280.  * declared here due to commonality.
  281.  */
  282. #define MAX_NETOBJ_SZ 1024 
  283. struct netobj {
  284.     u_int    n_len;
  285.     char    *n_bytes;
  286. };
  287. typedef struct netobj netobj;
  288. extern bool_t XDRFUN   xdr_netobj(XDR * xdrs, struct netobj * np);
  289.  
  290. /*
  291.  * These are the public routines for the various implementations of
  292.  * xdr streams.
  293.  */
  294. extern void   xdrmem_create(XDR * xdrs, caddr_t addr, u_int size,
  295.                 enum xdr_op op);    /* XDR using memory buffers */
  296. extern void   xdrstdio_create(XDR * xdrs, FILE * file, 
  297.                   enum xdr_op op);    /* XDR using stdio library */
  298. extern void   xdrrec_create(XDR * xdrs, u_int sendsize, u_int recvsize,
  299.                 void * tcp_handle,
  300.                 int (* readit)(void *, caddr_t, int),
  301.                 int (* writeit)(void *, caddr_t, int)); /* XDR pseudo records for tcp */
  302. extern bool_t xdrrec_endofrecord(XDR * xdrs, int sendnow);/* make end of xdr record */
  303. extern bool_t xdrrec_skiprecord(XDR * xdrs);/* move to beginning of next record */
  304. extern bool_t xdrrec_eof(XDR * xdrs);    /* true if no more input */
  305.  
  306.  
  307. #endif /* !__XDR_HEADER__ */
  308.